package leetcode.D900.T870;

import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-02-17 15:25:56 星期四
 */

class Solution {
    public int[] advantageCount(int[] nums1, int[] nums2) {
        int[] result = new int[nums1.length];
        PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((int[] left, int[] right) -> {
            return right[1] - left[1];
        });
        for (int i = 0; i < nums2.length; i++) {
            priorityQueue.add(new int[]{i, nums2[i]});
        }
        Arrays.sort(nums1);
        int i = 0, j = nums1.length-1;
        while (!priorityQueue.isEmpty()) {
            int[] top = priorityQueue.poll();
            if (top[1] < nums1[j]) {
                result[top[0]] = nums1[j--];
            } else {
                result[top[0]] = nums1[i++];
            }
        }
        return result;
    }
}
